﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VideoClipNameHelper
{
    public class GeneralConfig
    {
        //================================================================
        public static bool ParseConfigLines(List<string> lines, Dictionary<string, string> configData, List<string> textLines = null)
        {
            //
            try
            {
                lines.ForEach(x => x.Trim());
                lines.Remove("");
                lines.RemoveAll(x => x.StartsWith("#"));
                lines.RemoveAll(x => x.StartsWith("//"));
                if (null != textLines)
                {
                    textLines.Clear();
                    lines.Where(x => !x.Contains("=")).ToList().ForEach(x => textLines.Add(x));
                }
                lines.RemoveAll(x => !x.Contains("="));
                if (0 == lines.Count())
                {
                    return false;
                }
                var keyValues = (from x in lines let pp = x.Split(new char[] { '=' }, 2) select new { key = pp[0].Trim().ToLower(), value = pp[1].Trim() }).ToList();
                keyValues.RemoveAll(x => "" == x.key);
                configData.Clear();
                //
                // keys wewnętrznie są LCASE
                keyValues.ForEach(x => configData.Add(x.key, x.value));
            }
            catch (Exception e)
            {
                CLogger.Write(e.Message);
                configData.Clear();
                return false;
            }
            //
            return true;
        }
        //================================================================
        //================================================================
        public static string GetValueString(Dictionary<string, string> configData, string key, string defaultValue = "")
        {
            string value;
            if (configData.TryGetValue(key.ToLower(), out value))
            {
                return value;
            }
            return defaultValue;
        }
        //----------------------------------------------------------------
        public static bool GetValueBool(Dictionary<string, string> configData, string key, bool defaultValue = false)
        {
            string b = GetValueString(configData, key).ToLower();
            //
            if ("" == b) return defaultValue;
            //
            if ("true" == b || "1" == b) return true;
            if ("false" == b || "0" == b) return false;
            //
            return defaultValue;
        }
        //----------------------------------------------------------------
        public static KeyValuePair<string,string> GetKeyValuePair(Dictionary<string, string> configData, string key)
        {
            string value;
            if (configData.TryGetValue(key.ToLower(), out value))
            {
                return new KeyValuePair<string, string>(key.ToLower(), value);
            }
            return new KeyValuePair<string, string>("", "");
        }
        //================================================================
    }//class
}//namepace
